package cn.edu.zafu.bigdata.lyq.work8.we_re;

import java.io.*;
import java.util.*;

class Student implements Externalizable {
    private String id;
    private String name;
    private String phone;

    public Student() {}

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(id);
        out.writeObject(name);
        out.writeObject(phone);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.id = (String) in.readObject();
        this.name = (String) in.readObject();
        this.phone = (String) in.readObject();
    }

    @Override
    public String toString() {
        return "Student{id='" + id + "', name='" + name + "', phone='" + phone + "'}";
    }
}

class StudentManager implements Serializable {
    private static final long serialVersionUID = 1L;
    private Map<String, Student> students = new HashMap<>();
    private File file = new File("students.dat");

    public void load() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            while (true) {
                try {
                    // 强制类型转换，确保读取的是 Student 对象
                    Student student = (Student) ois.readObject();
                    students.put(student.getId(), student);
                } catch (EOFException e) {
                    break; // 文件结束
                } catch (ClassCastException e) {
                    System.err.println("Error: Attempted to cast a non-Student object to Student. Corrupted file?");
                    // 可以选择抛出异常、记录日志或清空当前文件内容
                    // 这里简单处理为清空 students Map 并停止加载
                    students.clear();
                    return;
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void save() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            for (Student student : students.values()) {
                oos.writeObject(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addStudent(Student student) {
        students.put(student.getId(), student);
    }

    public Student getStudent(String id) {
        return students.get(id);
    }

    public void updateStudent(Student student) {
        students.put(student.getId(), student);
    }

    public void deleteStudent(String id) {
        students.remove(id);
    }

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudentInfo(scanner);
                    break;
                case 2:
                    retrieveStudentInfo(scanner);
                    break;
                case 3:
                    updateStudentInfo(scanner);
                    break;
                case 4:
                    deleteStudentInfo(scanner);
                    break;
                case 5:
                    save(); // 在退出前保存学生信息
                    System.out.println("程序已退出。");
                    return;
                default:
                    System.out.println("无效的操作编号，请重新输入。");
            }
        }
    }

    private void addStudentInfo(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        addStudent(new Student(id, name, phone));
        System.out.println("学生信息已添加。");
    }

    private void retrieveStudentInfo(Scanner scanner) {
        while (true) {
            System.out.print("请输入要检索的学号 (输入QUIT退出): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }
            String id = input;
            Student student = getStudent(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生的信息。");
            }
        }
    }

    private void updateStudentInfo(Scanner scanner) {
        while (true) {
            System.out.print("请输入要更新的学号: ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }
            String id = input;
            Student student = getStudent(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                updateStudent(new Student(id, name, phone));
                System.out.println("学生信息已更新。");
            } else {
                System.out.println("未找到该学生的信息。");
            }
        }
    }

    private void deleteStudentInfo(Scanner scanner) {
        while (true) {
            System.out.print("请输入要删除的学号 (输入QUIT退出): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }
            String id = input;
            deleteStudent(id);
            System.out.println("学生信息已删除。");
        }
    }
}

public class StudentManagementSystem {
    public static void main(String[] args) {
        StudentManager manager = new StudentManager();
        manager.load(); // 程序启动时加载学生信息
        manager.showMenu(); // 显示菜单并处理用户输入
    }
}